import request from '@/utils/request/request';
import {
  BusinessColumnRecord,
  BusinessEntityAddParams,
  BusinessEntityListRes,
  BusinessEntityParams,
  BusinessEntityRecord,
  SQLRelevanceEntityRecord,
} from '@/api/infrastructure/model/businessEntityModal';

/**
 * @description 根据数据源和表名称获取列
 * @param dataSourceConfigId 数据源
 * @param name 表名称
 */
export function getDbTableColumns(dataSourceConfigId: any, tableName: any) {
  return request.get('/api/infra/codegen/db/column/list', {
    params: {
      dataSourceConfigId,
      tableName,
    },
  });
}

// 分页查询业务实体
export function queryBusinessEntityRecordList(params: BusinessEntityParams) {
  return request.get<BusinessEntityListRes>('/api/infra/form-bo/entity/page', {
    params,
  });
}

// 新增业务实体
export function addBusinessEntityRecord(data: BusinessEntityAddParams) {
  return request.post('/api/infra/form-bo', data);
}

// 修改业务实体基本信息（主表）
export function updateBusinessEntityRecord(data: BusinessEntityRecord) {
  return request.put('/api/infra/form-bo/entity', data);
}

// 修改业务实体字段信息（子表）
export function updateBusinessEntityColumnRecord(
  id: string,
  data: BusinessColumnRecord[]
) {
  return request.put(`/api/infra/form-bo/attr/${id}`, data);
}

// 根据业务实体id查业务实体字段信息
export function getBusinessEntityColumnRecordById(id: string) {
  return request.get(`/api/infra/form-bo/attr/${id}`);
}

// 根据业务实体id查业务实体字段关联关系
export function getBusinessEntityAssociationRecordById(id: string) {
  return request.get(`/api/infra/form-bo/table/${id}`);
}

// 删除业务实体
export function deleteBusinessEntityRecord(id: any) {
  return request.delete(`/api/infra/form-bo/${id}`);
}

// 查询业务实体与SQL对应关系数据列表
export function querySQLRelevanceEntityRecordList(
  params: SQLRelevanceEntityRecord
) {
  return request.get<SQLRelevanceEntityRecord[]>('/api/infra/form-sql/list', {
    params,
  });
}

// 新增业务实体与SQL对应关系数据列表
export function addSQLRelevanceEntityRecord(data: SQLRelevanceEntityRecord) {
  return request.post('/api/infra/form-sql', data);
}

// 修改业务实体与SQL对应关系数据列表
export function editSQLRelevanceEntityRecord(data: SQLRelevanceEntityRecord) {
  return request.put('/api/infra/form-sql', data);
}

// 执行sql语句
export function executeSQLRelevanceEntityRecord(id: string, data: any) {
  return request.post(`/api/infra/form-sql/execute/${id}`, data);
}

// 删除业务实体与SQL对应关系数据
export function deleteSQLRelevanceEntityRecord(id: any) {
  return request.delete(`/api/infra/form-sql/${id}`);
}
